package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionDecryptService;
import com.bidevalution.dao.entity.BeSectionDecrypt;
import com.qtp.common.core.ContextHolderUtils;
import com.qtp.common.core.TSUser;
import com.qtp.core.model.Paging;
import com.qtp.core.model.Response;
import com.qtp.core.util.DateUtil;
import com.qtp.user.models.SysParams;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 标书解密控制层
 * @author shenh@glodon.com
 * @date 2019-05-06 10:04
 */
@Log4j2
@RestController
@RequestMapping("/beSectionDecrypt")
public class BeSectionDecryptController extends BaseController {

    @Autowired
    private BeSectionDecryptService beSectionDecryptService;
	@Value("${SystemIdentify}")
	private String systemIdentify;

    /**
     * 获取单个投标单位解密信息
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getBeSectionDecryptInfo", method = RequestMethod.POST)
    @ResponseBody
    public Response<BeSectionDecrypt> getBeSectionDecryptInfo(@RequestParam("sectionId")String sectionId,@RequestParam("orgCode")String orgCode) {
        TSUser tsUser = getTSUser();
        try {
        	BeSectionDecrypt beSectionDecrypt = beSectionDecryptService.getBeSectionDecryptInfo(sectionId,orgCode);
        	//添加
//        	
//        	BeSectionChatroom beSectionChatroom = beSectionChatroomService.selectByPrimaryKey(sectionId);
//        	if(beSectionChatroom != null) {
//        		//
//        		ChatGroupInfoDTO chatGroupInfoDTO = chatGroupInfoApiService.getGroupInfoByGroupId(beSectionChatroom.getGroupId());
//            	chatGroupInfoApiService.saveGroup(systemIdentify, chatGroupInfoDTO.getGroupName(), beSectionDecrypt.getDecryptTime(), null, sectionId);
//            	
//        	}
            return Response.ok(beSectionDecrypt);
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }
    /**
     * 获取已经缴纳保证金单位数
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getPayDespiteCount", method = RequestMethod.POST)
    @ResponseBody
    public Response<Long> getPayDespiteCount(@RequestParam("sectionId")String sectionId) {
        try {
   
            return Response.ok(beSectionDecryptService.getPayDespiteCount(sectionId));
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }
    /**
     * 获取投标单位解密信息列表
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getBeSectionDecryptList", method = RequestMethod.POST)
    @ResponseBody
    public void getBeSectionDecryptList(HttpServletResponse response, @RequestParam("sectionId")String sectionId,
                                                                            @RequestParam("page")int page, @RequestParam("rp")int rp) {
        try {
            setHttpServletResponse(response);
            Paging<List<BeSectionDecrypt>> btnResponse = beSectionDecryptService.getBeSectionDecryptList(sectionId, page, rp);
            long total = btnResponse.getTotal();
            ContextHolderUtils.outGridJson(response, btnResponse.getData(), total,"yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 获取已解密的投标文件数量
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getDecryptedCount", method = RequestMethod.POST)
    @ResponseBody
    public Response<Long> getDecryptedCount(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionDecryptService.getDecryptedCount(sectionId));
        } catch (Exception e){
            return  Response.fail(" 获取已解密的投标文件数量失败");
        }
    }

    /**
     * 保存解密信息
     * @param sectionId 标段Id
     * @param keyValue ca解密后的数据
     * @return
     */
    @RequestMapping(value = "/saveKeyValue", method = RequestMethod.POST)
    @ResponseBody
    public Response<Integer> saveKeyValue(@RequestParam("sectionId")String sectionId, @RequestParam("keyValue")String keyValue, @RequestParam("orgCode")String orgCode) {
        try {
            return Response.ok(beSectionDecryptService.saveKeyValue(sectionId,orgCode,keyValue));
        } catch (Exception e){
            return  Response.fail("保存解密信息失败");
        }
    }

    /**
     * 获取设置的参数
     * @return
     */
    @RequestMapping(value = "/getOneSysParams", method = RequestMethod.POST)
    @ResponseBody
    public Response<String> getOneSysParams(@RequestParam("patpCode")String patpCode) {
        try {
            SysParams sysParams = beSectionDecryptService.getOneSysParams(patpCode);
            if(sysParams != null){
                return Response.ok(sysParams.getSypaValue());
            } else {
                return Response.fail("请在后台参数管理中设置参数");
            }
        } catch (Exception e){
            return  Response.fail("获取后台参数失败");
        }
    }

    /**
     * 获取解密开始时间
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getDecryptedStartTime", method = RequestMethod.POST)
    @ResponseBody
    public Response<String> getDecryptedStartTime(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionDecryptService.getDecryptedStartTime(sectionId));
        } catch (Exception e){
            return  Response.fail("获取解密开始时间失败");
        }
    }

    /**
     * 更新备份文件导入状态
     * @param sectionId 标段Id
     * @param importState 导入状态
     * @return
     */
    @RequestMapping(value = "/updateImportState", method = RequestMethod.POST)
    @ResponseBody
    public Response<Integer> updateImportState(@RequestParam("sectionId")String sectionId, @RequestParam("importState")Integer importState, @RequestParam("orgCode")String orgCode) {
        try {
            return Response.ok(beSectionDecryptService.updateImportState(sectionId,orgCode,importState));
        } catch (Exception e){
            return  Response.fail("保存解密信息失败");
        }
    }

    /**
     * 更新文件解密失败次数
     * @param sectionId 标段Id
     * @param importNum 失败次数
     * @return
     */
    @RequestMapping(value = "/updateImportNum", method = RequestMethod.POST)
    @ResponseBody
    public Response<Integer> updateImportNum(@RequestParam("sectionId")String sectionId, @RequestParam("importNum")Integer importNum, @RequestParam("orgCode")String orgCode) {
        try {
            return Response.ok(beSectionDecryptService.updateImportNum(sectionId,orgCode,importNum));
        } catch (Exception e){
            return  Response.fail("保存解密信息失败");
        }
    }


    /**
     * 保存加密标书密码信息
     * @param sectionId 标段Id
     * @param password ca解密后的数据
     * @return
     */
    @RequestMapping(value = "/savePassword", method = RequestMethod.POST)
    @ResponseBody
    public Response<Integer> savePassword(@RequestParam("sectionId")String sectionId, @RequestParam("password")String password, @RequestParam("orgCode")String orgCode) {
        try {
            return Response.ok(beSectionDecryptService.savePassword(sectionId,orgCode,password));
        } catch (Exception e){
            return  Response.fail("保存解密信息失败");
        }
    }

    /**
     * 判断是否过了标书解密截止时间
     *
     * @param sectionId
     * @return
     */
    @RequestMapping(value = "/isDecryptTimeout", method = RequestMethod.GET)
    @ResponseBody
    public Response<String> isDecryptTimeout(@RequestParam("sectionId") String sectionId) {
        try {
            //标书解密开始时间
            String decryptStartTime = beSectionDecryptService.getDecryptedStartTime(sectionId);
            //标书解密设置时间   单位分钟
            SysParams sysParams = beSectionDecryptService.getOneSysParams("decryptTime");
            if (sysParams != null && StringUtils.isNotBlank(decryptStartTime)) {
                //标书解密截止时间戳
                Long endTime = DateUtil.stringToDate(decryptStartTime, DateUtil.PT2).getTime() + Long.parseLong(sysParams.getSypaValue()) * 60 * 1000;
                if (endTime - DateUtil.getCurrentTime() < 0) {
                    return Response.fail("解密时间已过，不允许再次解密！");
                }
            } else {
                return Response.fail("解密开始时间或解密时间为空！");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("系统异常，请联系管理员！");
        }
        return Response.ok("还在解密截止时间内");
    }
    
   
}
